package flc.ast.activity;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import VideoHandle.EpEditor;
import VideoHandle.OnEditorListener;
import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.AudioFormatAdapter;
import flc.ast.bean.FormatBean;
import flc.ast.databinding.ActivityAudioFormatBinding;
import flc.ast.dialog.AudioNameDialog;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.media.audio.AudioPlayerImpl;
import stark.common.basic.media.audio.IAudioPlayer;
import stark.common.basic.utils.FileP2pUtil;

public class AudioFormatActivity extends BaseAc<ActivityAudioFormatBinding> implements IAudioPlayer.IListener{

    public static String audioPath;
    protected IAudioPlayer mAudioPlayer;
    private AudioFormatAdapter formatAdapter;
    private int oldPosition;
    private String mSelFormat = "mp3";//选择的格式
    private String mAudioName;
    private AudioNameDialog audioNameDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_audio_format;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivAudioFormatBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivPlay.setOnClickListener(this);
        mDataBinding.tvConvert.setOnClickListener(this);

        mAudioPlayer = new AudioPlayerImpl();
        mAudioPlayer.play(audioPath);
        mAudioPlayer.setListener(this);

        mDataBinding.rvFormatList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        formatAdapter = new AudioFormatAdapter();
        mDataBinding.rvFormatList.setAdapter(formatAdapter);
        formatAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivPlay) {//播放
            if (mAudioPlayer == null) {
                return;
            }
            if (mAudioPlayer.isPlaying()) {
                mAudioPlayer.pause();
            } else {
                mAudioPlayer.resume();
            }
        } else if (id == R.id.tvConvert) {//立即转换
            showAudioNameDialog();
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        formatAdapter.getItem(oldPosition).setSelect(false);
        formatAdapter.getItem(position).setSelect(true);
        formatAdapter.notifyDataSetChanged();
        oldPosition = position;
        mSelFormat = formatAdapter.getItem(position).getName();
    }

    @Override
    protected void initData() {
        mAudioName = FileUtils.getFileNameNoExtension(audioPath);
        mDataBinding.tvName.setText(FileUtils.getFileName(audioPath));
        List<FormatBean> listFormat = new ArrayList<>();
        listFormat.add(new FormatBean(getString(R.string.audio_format1), true));
        listFormat.add(new FormatBean(getString(R.string.audio_format2), false));
        listFormat.add(new FormatBean(getString(R.string.audio_format3), false));
        listFormat.add(new FormatBean(getString(R.string.audio_format4), false));
        formatAdapter.setList(listFormat);
    }

    private void showAudioNameDialog() {
        if (audioNameDialog == null) audioNameDialog = new AudioNameDialog(mContext);
        audioNameDialog.name = mAudioName;
        audioNameDialog.setListener(new AudioNameDialog.Ilistener() {
            @Override
            public void clickRename(String name) {
                mAudioName = name;
                audioConvert();
            }
        });
        audioNameDialog.show();
    }

    private void audioConvert() {
        showDialog(getString(com.stark.common.res.R.string.saving));
        String path = PathUtils.getFilesPathExternalFirst() + Content.myTemp + "/" + mAudioName + "." + mSelFormat;

        EpEditor.audioFormatConvert(audioPath, path, new OnEditorListener() {
            @Override
            public void onSuccess() {
                dismissDialog();
                ToastUtils.showShort(getString(com.stark.common.res.R.string.save_success));
                FileP2pUtil.copyPrivateAudioToPublic(mContext, path);
                startActivity(HomeActivity.class);
            }

            @Override
            public void onFailure() {
                dismissDialog();
                ToastUtils.showShort(R.string.format_failure);
            }

            @Override
            public void onProgress(float progress) {

            }
        });
    }

    @Override
    public void onUpdatePlayTime(int curPlayTime, int duration) {

    }

    @Override
    public void onPlayChange(boolean isPlaying) {
        if (isPlaying) {
            mDataBinding.ivPlay.setImageResource(R.drawable.abf2);
        } else {
            mDataBinding.ivPlay.setImageResource(R.drawable.azt);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAudioPlayer != null) {
            mAudioPlayer.resume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mAudioPlayer != null) {
            mAudioPlayer.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAudioPlayer != null) {
            mAudioPlayer.stop();
        }
        FileUtils.deleteAllInDir(PathUtils.getFilesPathExternalFirst() + Content.myTemp);
    }

}